<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;
use App\Models\UserModels\User;
use App\Models\DylistModels\DyTopic;
use App\Models\DylistModels\DyBangdanVideo;
use App\Models\DylistModels\DyHotVideo;
use App\Models\DylistModels\DySport;
use App\Models\DylistModels\DyAmusement;
use App\Models\DylistModels\DyGame;
use App\Models\DylistModels\DyFood;
use App\Models\DylistModels\DyDrama;
use App\Models\DylistModels\DyCar;
use App\Models\DylistModels\DyTravel;
use App\Models\DylistModels\DyCospa;
use App\Models\DylistModels\DyStars;
use App\Models\DylistModels\DyHotsearchVideo;
use App\Models\DylistModels\DyHotsearchTrending;
use App\Models\DylistModels\DyHotsearchWord;
use App\Models\DyModels\DyuserQyhVideo;
use App\Models\DyModels\DyuserQyhFans;
use App\Models\UserModels\PrivateLetter;
use App\Models\UserModels\CommentLetter;
class DouyinListController extends Controller
{
    //抖音调用接口方法
    private $clientKey = '';
    private $clientSecret = '';
    private $url = '';
    private $redirectUri = '';
    private $scope = '';
    private $thirsdUrl = "";
    private $thirsdUrlTwo = "";

    function __construct(Request $request)
    {
        $this->parames = $request->all();
        $this->clientKey = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_client_key'));
        $this->clientSecret = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_client_secret'));
        $this->url = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_url'));
        $this->redirectUri = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_bank_url'));

        $this->scope = "video.create,video.delete,video.data,video.list,im.share,video.search,video.search.comment,aweme.share,user_info,following.list,fans.list,fans.check,renew_refresh_token,mobile_alert,mobile,video.comment,enterprise.data,enterprise.im,item.comment,data.external.user,data.external.item,fans.data,star_top_score_display,star_tops,star_author_score_display,data.external.sdk_share,data.external.fans_favourite,poi.search,micapp.is_legal,incremental_authorization";

        $this->thirsdUrl = "http://api.xstatus.cn/douyin/video/comments?token=3440488a80744d30abe1a41f3a7062d5&aweme_id=";
        $this->thirsdUrlTwo = "http://api.xstatus.cn/douyin/video/comment/reply?token=3440488a80744d30abe1a41f3a7062d5&aweme_id=";
    }

    //生成cltoken
    function createcltoken()
    {
        $DyToools = new DyToools();
        $clienttk = $DyToools->client_token();
        return $clienttk;
    }
    //回复评论的方法
    public function sendToCommnet($date){
        $partner_id = session("vip_user")[0]['admin_id'];
        $data = $this->createcltoken();
        $access_token = $data['data']['access_token'];
        $url = $this->url . "/video/comment/reply/?open_id={$date['popenid']}&access_token=$access_token";
        $date=array(
            'popenid' => $date['popenid'],
            'comment_id' => $date['comment_id'],
            'content' => $date['content'],
            'item_id' => $date['item_id'],
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        $data = [
            'content' => $date['content'],
            'item_id' => $date['item_id'],
            'comment_id' => $date['comment_id'],
        ];
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen(json_encode($data)))
        );
        $reply = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec 返回TRUE 设置  curl_exec 返回json(此处) 失败都返回FALSE
        curl_close($ch);
        //回复入库
        $this->saveprivateletter($date);//把私信数据入库
        return json_decode($reply, true);
    }
    //给单个粉丝/关注发送私信（企业号专有属性）
    public function sendToFans($open_id,$content,$to_user_id,$message_type)
    {
        //接口调用 openid为要发送的客户的id 所以在打开页面的时候方法里面最好直接传上参数
        $data = $this->createcltoken();
        $access_token = $data['data']['access_token'];
       // foreach ()
        $url = $this->url . "/enterprise/im/message/send/?open_id={$open_id}&access_token=$access_token";
        $date=array(
            'atkoen' =>$access_token,
            'popenid' =>$open_id,
            'content' =>$content,
            'openid' => $to_user_id,
            'mtype' =>$message_type,
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        $data = [
            'content' => json_encode(['text'=>"$content"]),
            'message_type' => $message_type,
            'to_user_id' => $to_user_id,
        ];
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen(json_encode($data)))
        );
        $reply = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec 返回TRUE 设置  curl_exec 返回json(此处) 失败都返回FALSE
        curl_close($ch);
        //回复入库
        $this->saveCommentReplay($date);//把私信数据入库
        return json_decode($reply, true);
    }
//私信记录的方法 openid为传过来的接受者id pid-账号操作者id
    public function saveprivateletter($date)
    {
        $partner_id = session("vip_user")[0]['admin_id'];
        $soPrivateLetter = new PrivateLetter();
        $data = array(
            'popenid' => $date['popenid'],
            'toopenid' => $date['openid'],
            'pid' => $partner_id,
            'content' => $date['content'],
            'type' => $date['mtype'],
            'create_time' => date('Y-m-d H:i:s', time()),
        );
        $soPrivateLetter->db_insertGetId($data);
    }
    //评论入库方法
    public function saveCommentReplay($date){
        $soCommentLetter=new CommentLetter();
        $data=array(
            'popenid' => $date['popenid'],
            'comment_id' => $date['comment_id'],
            'content' => $date['content'],
            'item_id' => $date['item_id'],
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        );
        $soCommentLetter->db_insertGetId($data);
    }
    /**    刷新access_token
     *    流程 ： 其他接口返回 2190008 或 10008   需要调用此方法
     *    如果此方法返回  10010 这里有问题官方文档是说直接重新授权    （需要调用 refresh_token  方法刷新）
     */
    function refresh_token($refresh_token)
    {
        $url = $this->url . "/oauth/refresh_token/?client_key={$this->clientKey}&grant_type=refresh_token&refresh_token={$refresh_token}";
        $data = $this->getJson($url);
        return $data;
    }

    //获取所有的视频的方法
    function getMoreVideolist($open_id, $access_token, $cursor)
    {
        $soDyuserQyhVideo = new DyuserQyhVideo();
        $url = $this->url . "/video/list/?open_id=$open_id&access_token=$access_token&cursor=$cursor&count=20";//获取近一个月的视频
        $data = $this->getJson($url);
        if ($data['data']['has_more'] == 1) {
            //先数据入库然后再次调用自己
            $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $open_id);
            $this->getMoreVideolist($open_id, $access_token, $data['data']['cursor']);
        } else {
            $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $open_id);
        }
    }
    //获取企业用户视频数据
    function getEnterpriseinfo(Request $request)
    {
        $soDyuserQyhVideo = new DyuserQyhVideo();
        $soUser = new User();
        $infoToken = new Douyin();
        $DyToools = new DyToools();
        $openid = $soUser->getuserinfo();//获取openid和对应的access_token以及refresh_token
        foreach ($openid as $k => $v) {
            if (!empty($v['access_token'])) {
                $url = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$v['access_token']}&count=20";//获取近一个月的视频
                $data = $this->getJson($url);
                if ($data['extra']['error_code'] == 0) {
                    // 返回值正确的话  数据入库
                    $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $v['open_id']);
                    if ($data['data']['has_more'] == 1) {
                        //如果还有更多视频没拉取调用方法拉取
                        $this->getMoreVideolist($v['open_id'], $v['access_token'], $data['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                    }
                } elseif (isset($dataList['data']['error_code']) && ($dataList['data']['error_code'] == 10008 || $dataList['data']['error_code'] == 2190008)) {
                    //授权过期刷新access_token然后继续
                    $ass = $this->refresh_token($v['refresh_token']);//刷新access——token
                    $infoToken->upToken($ass['data'], $v['open_id']);//更新数据库的信息
                    //重新调用获取数据的方法
                    if ($ass['data']['error_code'] == 0) { //如果返回值正确就进行接口调用
                        $url = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$ass['data']['access_token']}&count=20";//获取近一个月的视频
                        $date = $this->getJson($url);
                        if ($date['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                            $soDyuserQyhVideo->saveVideoInfo($date['data']['list'], $v['open_id']);
                            if ($date['data']['has_more'] == 1) {
                                //如果还有更多视频没拉取调用方法拉取
                                $this->getMoreVideolist($v['open_id'], $ass['data']['access_token'], $date['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                            }
                        }
                    } elseif (isset($ass['data']['error_code']) && $ass['data']['error_code'] == 10010) {//如果之前刷新也失败返回这个就调用方法目前无效 需重新授权
                        //refresh_token 过期
                        $dataTwo = $DyToools->renew_refresh_token($v['refresh_token']);//刷新refresh_token
                        if (isset($data['data']['error_code']) && $dataTwo['data']['error_code'] == 0) {
                            //更新数据库的 refresh_token
                            $infoToken->upRef($dataTwo['data'], $v['open_id']);
                            //用新的来更新access_token 然后继续调用数据获取下视频数据
                            $acss = $this->refresh_token($dataTwo['data']['refresh_token']);//刷新access——token
                            $infoToken->upToken($acss['data'], $v['open_id']);//更新数据库的信息
                            $urel = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$acss['data']['access_token']}&count=20";//获取近一个月的视频
                            $datt = $this->getJson($urel);
                            if ($datt['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                                $soDyuserQyhVideo->saveVideoInfo($datt['data']['list'], $v['open_id']);
                                if ($datt['data']['has_more'] == 1) {
                                    //如果还有更多视频没拉取调用方法拉取
                                    $this->getMoreVideolist($v['open_id'], $acss['data']['access_token'], $datt['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                                } else {
                                    $infoToken->upTokenStatus($v['open_id']);//授权过期需用户重新授权 更改授权状态 为 0
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //获取企业用户粉丝数据
    function getQyhFansinfo(Request $request)
    {
        $soDyuserQyhFans = new DyuserQyhFans();
        $soUser = new User();
        $infoToken = new Douyin();
        $DyToools = new DyToools();
        $openid = $soUser->getuserinfo();//获取openid和对应的access_token以及refresh_token
        foreach ($openid as $k => $v) {
            if (!empty($v['access_token'])) {
                $url = $this->url . "/fans/list/?open_id={$v['open_id']}&access_token={$v['access_token']}&count=20";//获取近一个月的视频
                $data = $this->getJson($url);
                if ($data['extra']['error_code'] == 0) {
                    // 返回值正确的话  数据入库
                    $soDyuserQyhFans->saveFansInfo($data['data']['list'], $v['open_id']);
                    if ($data['data']['has_more'] == 1) {
                        //如果还有更多视频没拉取调用方法拉取
                        $this->getAllinfo($v['open_id'], $v['access_token'], $data['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                    }
                } elseif (isset($dataList['data']['error_code']) && ($dataList['data']['error_code'] == 10008 || $dataList['data']['error_code'] == 2190008)) {
                    //授权过期刷新access_token然后继续
                    $ass = $this->refresh_token($v['refresh_token']);//刷新access——token
                    $infoToken->upToken($ass['data'], $v['open_id']);//更新数据库的信息
                    //重新调用获取数据的方法
                    if ($ass['data']['error_code'] == 0) { //如果返回值正确就进行接口调用
                        $url = $this->url . "/fans/list/?open_id={$v['open_id']}&access_token={$ass['data']['access_token']}&count=20";//获取近一个月的视频
                        $date = $this->getJson($url);
                        if ($date['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                            $soDyuserQyhFans->saveFansInfo($date['data']['list'], $v['open_id']);
                            if ($date['data']['has_more'] == 1) {
                                //如果还有更多视频没拉取调用方法拉取
                                $this->getAllinfo($v['open_id'], $ass['data']['access_token'], $date['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                            }
                        }
                    } elseif (isset($ass['data']['error_code']) && $ass['data']['error_code'] == 10010) {//如果之前刷新也失败返回这个就调用方法目前无效 需重新授权
                        //refresh_token 过期
                        $dataTwo = $DyToools->renew_refresh_token($v['refresh_token']);//刷新refresh_token
                        if (isset($data['data']['error_code']) && $dataTwo['data']['error_code'] == 0) {

                            //更新数据库的 refresh_token
                            $infoToken->upRef($dataTwo['data'], $v['open_id']);
                            //用新的来更新access_token 然后继续调用数据获取下视频数据
                            $acss = $this->refresh_token($dataTwo['data']['refresh_token']);//刷新access——token
                            $infoToken->upToken($acss['data'], $v['open_id']);//更新数据库的信息
                            $urel = $this->url . "/fans/list/?open_id={$v['open_id']}&access_token={$acss['data']['access_token']}&count=20";//获取近一个月的视频
                            $datt = $this->getJson($urel);
                            if ($datt['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                                $soDyuserQyhFans->saveFansInfo($datt['data']['list'], $v['open_id']);
                                if ($datt['data']['has_more'] == 1) {
                                    //如果还有更多视频没拉取调用方法拉取
                                    $this->getAllinfo($v['open_id'], $acss['data']['access_token'], $datt['data']['cursor']);//如果有就调用函数把所有的数据都抓取出来
                                } else {
                                    $infoToken->upTokenStatus($v['open_id']);//授权过期需用户重新授权 更改授权状态 为 0
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //当存在新hasmore的时候使用的，无限调用自己知道数据抓取完
    function getAllinfo($open_id, $access_token, $cursor)
    {
        $soDyuserQyhFans = new DyuserQyhFans();
        $url = $this->url . "/fans/list/?open_id=$open_id&access_token=$access_token&cursor=$cursor&count=20";//获取粉丝数据
        $data = $this->getJson($url);
        //开始判断如果是还有再次调用自己
        if ($data['data']['has_more'] == 1) {
            //先数据入库然后再次调用自己
            $soDyuserQyhFans->saveFansInfo($data['data']['list'], $open_id);
            $this->getAllinfo($open_id, $access_token, $data['data']['cursor']);
        } else {
            $soDyuserQyhFans->saveFansInfo($data['data']['list'], $open_id);
        }
    }

    //热门素材总方法-------------------------------------------------------------------------------------------
    public function getPopularMaterial()
    {
        $data = $this->createcltoken();
        $access_token = $data['data']['access_token'];
        //  print_r($access_token);exit();
        if (!empty($access_token)) {
            $this->gethotsearch($access_token);          //获取抖音实时热点词
            $this->gettrendingsearch($access_token);    //获取抖音上升词
            $this->gethotSearchVideo($access_token);    //获取热度词聚合视频
            $this->gettopic($access_token);             //获取抖音话题数据 并入库（入库抖音话题榜）
            $this->gethotword($access_token);      //获取抖音热门视频内容
            $this->getStars($access_token);  //获取抖音娱乐明星数据
            $this->getDySport($access_token);  //抖音体育榜单
            $this->getAmusement($access_token);    //抖音搞笑榜单
            $this->getGame($access_token);    //抖音游戏资讯榜单
            $this->getFood($access_token);    //抖音美食资讯榜单
            $this->getDrama($access_token);    //抖音剧情资讯榜单
            $this->getCar($access_token);    //抖音汽车资讯榜单
            $this->getTravel($access_token);    //抖音旅游资讯榜单
            $this->getCospa($access_token);    //抖音二次元资讯榜单
        }

    }
    //获取抖音实时热点词
    function gethotsearch($access_token)
    {
        $DyHotsearchWord = new DyHotsearchWord();
        $url = $this->url . "/hotsearch/sentences/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $DyHotsearchWord->upHotsearchWord($data['data']['list']);
        }
        return $data;
    }
    //获取抖音上升词
    function gettrendingsearch($access_token)
    {
        $upDyHotsearchTrending = new DyHotsearchTrending();

        $url = $this->url . "/hotsearch/trending/sentences/?access_token=$access_token&cursor=0&count=20";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $upDyHotsearchTrending->upDyHotsearchTrending($data['data']['list']);
        }
        return $data;
    }
    //获取热度词聚合视频
    function gethotSearchVideo($access_token)
    {
        $DyHotsearchWord = new DyHotsearchWord();
        $date = $DyHotsearchWord->getword();
        if (is_array($date)) {
            foreach ($date as $k => $v) {
                $hot_sentence = $v['sentence'];
                $wid = $v['id'];
                $url = $this->url . "/hotsearch/videos/?access_token=$access_token&hot_sentence=" . urlencode($hot_sentence);
                $data = $this->getJson($url);
                //   echo '<pre/>';     print_r($data);
                $info = $data['data']['list'];

                if ($data['extra']['error_code'] == 0) {
                    foreach ($info as $kk => $vv) {
                        $data = array(
                            'wid' => $wid,
                            'share_url' => $vv['share_url'],
                            'title' => $vv['title'],
                            'cover' => $vv['cover'],
                            'item_id' => $vv['item_id'],
                            'video_status' => $vv['video_status'],
                            'comment_count' => $vv['statistics']['comment_count'],
                            'digg_count' => $vv['statistics']['digg_count'],
                            'download_count' => $vv['statistics']['download_count'],
                            'forward_count' => $vv['statistics']['forward_count'],
                            'play_count' => $vv['statistics']['play_count'],
                            'share_count' => $vv['statistics']['share_count'],
                            'create_time' => date('Y-m-d H:i:s', time()),
                            'update_time' => date('Y-m-d H:i:s', time()),
                        );
                        // echo '<pre/>';  print_r($data);exit();
                        //  $date=json_encode($data);
                        $DyHotsearchVideo = new DyHotsearchVideo();
                        $DyHotsearchVideo->upadinfo($data);
                    }

                }
            }
        }
    }
    //获取抖音话题数据 并入库（入库抖音话题榜）
    function gettopic($access_token)
    {
        $accessToken = new DyTopic();
        $url = $this->url . "/data/extern/billboard/topic/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upTopicto($data['data']['list']);
        }
    }
    //获取抖音娱乐明星数据
    function getStars($access_token)
    {
        $accessToken = new DyStars();
        $url = $this->url . "/data/extern/billboard/stars/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upStarsto($data['data']['list']);
        }
    }
    //抖音体育榜单
    function getDySport($access_token)
    {
        $accessToken = new DySport();
        $url = $this->url . "/data/extern/billboard/sport/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upDySportto($date);
        }

    }
    //获取抖音热门视频内容
    function gethotword($access_token)
    {
        $accessToken = new DyHotVideo();
        $url = $this->url . "/data/extern/billboard/hot_video/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upHotVideoto($date);
        }
        return false;
    }
    //抖音搞笑榜单
    function getAmusement($access_token)
    {
        $accessToken = new DyAmusement();
        $url = $this->url . "/data/extern/billboard/amusement/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upAmusementto($date);
        }
        return false;
    }
    //抖音游戏资讯榜单
    function getGame($access_token)
    {
        $accessToken = new DyGame();
        $url = $this->url . "/data/extern/billboard/game/inf/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upGameto($date);
        }
        return false;
    }
    //抖音美食榜榜单
    function getFood($access_token)
    {
        $accessToken = new DyFood();
        $url = $this->url . "/data/extern/billboard/food/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upFoodto($date);
        }
        return false;
    }
    //抖音剧情榜榜单
    function getDrama($access_token)
    {
        $accessToken = new DyDrama();
        $url = $this->url . "/data/extern/billboard/drama/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upDramato($date);
        }
        return false;
    }
    //抖音汽车榜榜单
    function getCar($access_token)
    {
        $accessToken = new DyCar();
        $url = $this->url . "/data/extern/billboard/car/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upCarto($date);
        }
        return false;
    }
    //抖音旅游榜榜单
    function getTravel($access_token)
    {
        $accessToken = new DyTravel();
        $url = $this->url . "/data/extern/billboard/travel/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upTravelto($date);
        }
        return false;
    }
    //抖音二次元榜榜单
    function getCospa($access_token)
    {
        $accessToken = new DyCospa();
        $url = $this->url . "/data/extern/billboard/cospa/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upCospato($date);
        }
        return false;
    }
    //接受请求 保存配置
    function saveConfigJson()
    {
        $url = str_replace("/public", '', $_SERVER['DOCUMENT_ROOT']);
        $body = file_get_contents("php://input");
        $body = json_decode($body, true);
        $path = $body['path'];
        $data = $body['data'];
        file_put_contents($url . $path, json_encode($data));
    }

    //get请求
    function getJson($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//为true，则会跟踪爬取重定向页面，否则，不会跟踪重定向页面
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

    //post请求
    function postJson($url, $data = array())
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //超时时间
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $json = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return json_decode($json, true);
    }
}